package com.vicene.gmail.order.config;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

@Configuration
public class MyRabbitConfig {
    @Autowired
    RabbitTemplate rabbitTemplate;

    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }

    /**
     * 发送端确认，增加确认回调,消息抵达broker ack 为true
     */
    //构造器调用完成后调用
    @PostConstruct
    public void initRabbitTemplate(){
        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
            @Override
            public void confirm(CorrelationData correlationData, boolean b, String s) {
                System.out.println("confirm....correlationData:      " + correlationData);
                System.out.println("confirm....b:      " + b);
                System.out.println("confirm....s:      " + s);

            }
        });

        //只要消息没有投递给队列，才会回调
        /**
         * message投递失败的详细信息，i 回复状态码 s 回复文本内容 s1 消息发给的交换机 s2 指定的路由件
         */
        rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback() {
            @Override
            public void returnedMessage(Message message, int i, String s, String s1, String s2) {
                System.out.println("returnedMessage....message:      " + message);
                System.out.println("returnedMessage....i:      " + i);
                System.out.println("returnedMessage....s:      " + s);
                System.out.println("returnedMessage....s1:      " + s1);

                System.out.println("returnedMessage....s2:      " + s2);

            }
        });
    }
}
